<template>
  <div class="table-page">
    <GiTable
      ref="tableRef"
      row-key="id"
      title="测试环境管理"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #custom-left>
        <a-input v-model="queryForm.projectId" placeholder="请输入所属项目ID" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-input v-model="queryForm.name" placeholder="请输入环境名称" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-input v-model="queryForm.protocol" placeholder="请输入协议" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-input v-model="queryForm.domain" placeholder="请输入环境域名" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-input v-model="queryForm.port" placeholder="请输入端口" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-input v-model="queryForm.createdBy" placeholder="请输入创建者" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-button @click="reset">重置</a-button>
      </template>
      <template #custom-right>
        <a-button v-permission="['testing:environment:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <span>新增</span>
        </a-button>
        <a-tooltip content="导出">
          <a-button v-permission="['testing:environment:export']" class="gi_hover_btn-border" @click="onExport">
            <template #icon>
              <icon-download />
            </template>
          </a-button>
        </a-tooltip>
      </template>
      <template #name="{ record }">
        <a-link @click="onDetail(record)">{{ record.name }}</a-link>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['testing:environment:update']" @click="onUpdate(record)">修改</a-link>
          <a-link
            v-permission="['testing:environment:delete']"
            status="danger"
            :disabled="record.disabled"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <EnvironmentAddModal ref="EnvironmentAddModalRef" @save-success="search" />
    <EnvironmentDetailDrawer ref="EnvironmentDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import EnvironmentAddModal from './EnvironmentAddModal.vue'
import EnvironmentDetailDrawer from './EnvironmentDetailDrawer.vue'
import { type EnvironmentResp, type EnvironmentQuery, deleteEnvironment, exportEnvironment, listEnvironment } from '@/apis'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'

defineOptions({ name: 'Environment' })

const queryForm = reactive<EnvironmentQuery>({
  projectId: '',
  name: '',
  protocol: '',
  domain: '',
  port: '',
  createdBy: '',
  sort: ['createdAt,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listEnvironment({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  { title: 'ID', dataIndex: 'id', slotName: 'id' },
  { title: '所属项目ID', dataIndex: 'projectId', slotName: 'projectId' },
  { title: '环境名称', dataIndex: 'name', slotName: 'name' },
  { title: '协议', dataIndex: 'protocol', slotName: 'protocol' },
  { title: '环境域名', dataIndex: 'domain', slotName: 'domain' },
  { title: '端口', dataIndex: 'port', slotName: 'port' },
  { title: '描述', dataIndex: 'description', slotName: 'description' },
  { title: '乐观锁', dataIndex: 'version', slotName: 'version' },
  { title: '业务状态', dataIndex: 'status', slotName: 'status' },
  { title: '逻辑删除 1: 已删除， 0: 未删除', dataIndex: 'isDeleted', slotName: 'isDeleted' },
  { title: '创建者', dataIndex: 'createdBy', slotName: 'createdBy' },
  { title: '更新者', dataIndex: 'updatedBy', slotName: 'updatedBy' },
  { title: '创建时间', dataIndex: 'createdAt', slotName: 'createdAt' },
  { title: '更新时间', dataIndex: 'updatedAt', slotName: 'updatedAt' },
  {
    title: '操作',
    slotName: 'action',
    width: 130,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['testing:environment:update', 'testing:environment:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.projectId = ''
  queryForm.name = ''
  queryForm.protocol = ''
  queryForm.domain = ''
  queryForm.port = ''
  queryForm.createdBy = ''
  search()
}

// 删除
const onDelete = (item: EnvironmentResp) => {
  return handleDelete(() => deleteEnvironment(item.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportEnvironment(queryForm))
}

const EnvironmentAddModalRef = ref<InstanceType<typeof EnvironmentAddModal>>()
// 新增
const onAdd = () => {
  EnvironmentAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (item: EnvironmentResp) => {
  EnvironmentAddModalRef.value?.onUpdate(item.id)
}

const EnvironmentDetailDrawerRef = ref<InstanceType<typeof EnvironmentDetailDrawer>>()
// 详情
const onDetail = (item: EnvironmentResp) => {
  EnvironmentDetailDrawerRef.value?.onDetail(item.id)
}
</script>

<style lang="scss" scoped></style>
